package com.huike.web.controller.system;

import com.huike.clues.service.ISysMenuService;
import com.huike.common.annotation.Log;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.TreeSelect;
import com.huike.common.core.domain.entity.SysMenu;
import com.huike.common.enums.BusinessType;
import com.huike.web.result.RoleMenuTreeAjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: wwq
 * @Date: 2023/07/11/15:34
 * @Description:
 */
@RestController
@RequestMapping("/system/menu")
@Slf4j
@Api(tags = "菜单信息相关接口")
public class SysMenuController extends BaseController {
    @Resource
    private ISysMenuService sysMenuService;

    @ApiOperation("新增菜单")
    @PostMapping
    @Log(title = "新增菜单", businessType = BusinessType.INSERT)
    public AjaxResult<?> add(@RequestBody SysMenu sysMenu) {
        sysMenuService.add(sysMenu);
        return AjaxResult.success();
    }

    @ApiOperation("修改菜单")
    @PutMapping
    @Log(title = "修改菜单", businessType = BusinessType.UPDATE)
    public AjaxResult<?> update(@RequestBody SysMenu sysMenu) {
        sysMenuService.update(sysMenu);
        return AjaxResult.success();
    }

    @ApiOperation("获取菜单列表")
    @GetMapping("/list")
    public AjaxResult<?> list(SysMenu sysMenu) {
        List<SysMenu> list = sysMenuService.list(sysMenu);
        return AjaxResult.success(list);
    }

    @ApiOperation("加载对应角色菜单列表树")
    @GetMapping("/roleMenuTreeselect/{roleId}")
    public RoleMenuTreeAjaxResult<TreeSelect> roleMenuTreeSelect(@PathVariable Long roleId) {
        TreeSelect treeSelect = sysMenuService.roleMenuTreeSelect(roleId);
        return RoleMenuTreeAjaxResult.success(treeSelect);
    }

    @ApiOperation("获取菜单下拉树列表")
    @GetMapping("/treeselect")
    public AjaxResult<List<TreeSelect>> treeselect(SysMenu sysMenu) {
        List<TreeSelect> list = sysMenuService.treeselect(sysMenu);
        return AjaxResult.success(list);
    }

    @ApiOperation("根据菜单编号获取详细信息")
    @GetMapping("/{menuId}")
    public AjaxResult<SysMenu> selectMenuById(@PathVariable Long menuId) {
        SysMenu sysMenu = sysMenuService.selectMenuById(menuId);
        return AjaxResult.success(sysMenu);
    }

    @ApiOperation("删除菜单.存在子菜单或已分配,不允许删除")
    @Log(title = "删除菜单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{menuId}")
    public AjaxResult<SysMenu> deleteMenuById(@PathVariable Long menuId) {
        sysMenuService.deleteMenuById(menuId);
        return AjaxResult.success();
    }
}
